/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package objects;

import communication.ClipRecord;
import drawables.DrawableManager;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaPlayer.Status;
import javafx.util.Duration;
import mediaCache.MediaCache;

/**
 *
 * @author Irzh
 */
public class MP3Clip extends AudioClip {
    
    double duration=0;
    transient MediaPlayer mediaPlayer;
    public MP3Clip(String audioUrl, String id , String title, String uplaoder, String instrument , String genre ,int bpm , String key, float rating) {
		super(audioUrl, id , title , uplaoder, instrument , genre , bpm , key, rating);
		
	}
    
    @Override
    public AudioClip clone() {
        AudioClip cloned = new MP3Clip(audioUrl, id , title , uplaoder, instrument , genre , bpm , key, rating);
        DrawableManager.get().add(cloned);
	return cloned;
    }

    @Override
    public void loadAudio(String url) {
        Media media = null;
        
        
        
        if(MediaCache.get().isMediaExists(id)){
            media = MediaCache.get().getMedia(id);
        }else{
            try {
                URL u = new URL(url);
                String s = u.getPath();
                
                media = new Media(url);
                MediaCache.get().addMedia(media, id);
            } catch (Exception ex) {
                
            }
        }
        mediaPlayer = new MediaPlayer(media);
        mediaPlayer.setOnReady(new Runnable() {

            @Override
            public void run() {
                if(loadingImage!=null){
                    loadingImage.setVisible(false);
                }
            }
        });
        
        
        mediaPlayer.setVolume(currentTrack.getVolume());
        mediaPlayer.setBalance(currentTrack.getBalance());
        ready = true;
        
        
    }

    @Override
    public void play() {
        //mediaPlayer.setAutoPlay(true);
        mediaPlayer.play();
        
    }

    @Override
    public void stop() {
        mediaPlayer.stop();
    }

    @Override
    public double getDuration() {
        double res = 0;
        if(ready && duration==0){
            Duration d = mediaPlayer.getTotalDuration();
            res = mediaPlayer.getTotalDuration().toSeconds();
            if(!Double.isNaN(res))
                duration = res;
        }
        return duration;
    }
    
    
    public static AudioClip createFromRecord(ClipRecord cl){
        String newId = cl.getId();
        try {
            newId = newId.replace("clipRecord(\"", "");
            newId = newId.replace("\")", "");
        } catch (Exception e) {
        }
        
        MP3Clip c = new MP3Clip("http://sharecomposition.appspot.com/serve?blob-key=" + newId, newId, cl.getInfo(), cl.getUploader(), cl.getInstrument(), cl.getGenre(), Integer.valueOf(cl.getTempo()) , cl.getKey() , 5);
        c.setStartTime(cl.getStartTime());
        return c;
    }

    @Override
    public void previewPlay() {
        Media media = null;
        media = new Media(audioUrl);
        MediaPlayer prevMediaPlayer = new MediaPlayer(media);
        prevMediaPlayer.play();
    }
    
    @Override
    public void setVolume(double volume){
        mediaPlayer.setVolume(volume);
    }

    @Override
    public void setPan(double balance) {
        mediaPlayer.setBalance(balance);
    }
}
